package com.yhm.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.yhm.dao.AllDAO;
import com.yhm.model.Dep;
import com.yhm.model.Redo;
import com.yhm.model.Trace;
import com.yhm.service.RedoManager;
import com.yhm.util.page.RedoPage;

@Component("redoManager")
public class RedoManagerImpl implements RedoManager{

	private AllDAO allDAO = new AllDAO();
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 获取重做列表
	 */
	@SuppressWarnings("unchecked")
	public String get_redo_list(RedoPage page){
		String json = "";
		String string_redo = "";
		
		if(!page.getDep().equals("0")){
				string_redo =  "where " + "r.department =" + " " + "'" + page.getDep()
						+ "'";
		}
		
		String string_order = "";
		if(page.getBegin_time().length()!=0&&page.getEnd_time().length()!=0){
			if(string_redo.length()!=0)
				string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+page.getEnd_time()+"'))";
			else
				string_order = "where r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+page.getEnd_time()+"'))";
		}
		if(page.getBegin_time().length()==0&&page.getEnd_time().length()!=0){
			if(string_redo.length()!=0)	
				string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '2012-10-1' and '"+page.getEnd_time()+"'))";
			else
				string_order = "where r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '2012-10-1' and '"+page.getEnd_time()+"'))";
		}
		if(page.getBegin_time().length()!=0&&page.getEnd_time().length()==0){
			Date date = new Date();
			String now = df.format(date);
			if(string_redo.length()!=0)	
				string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+now+"'))";
			else
				string_order = "where r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+page.getBegin_time()+"' and '"+now+"'))";
		}	
		String hql = "from Redo r";
		if(string_redo.length()!=0)  hql = hql +" "+ string_redo;
		if(string_order.length()!=0)  hql = hql +" "+ string_order;
		//System.out.println(hql);
		List<?> redo_lists = allDAO.getRedoDAO().findhql("select COUNT(*) " + hql);
		int redo_lists_size = ((Long) redo_lists.get(0)).intValue();
		if(redo_lists_size == 0){
			json = "{\"total\":0,\"rows\":[]}";
			return json;
		}
		page.countPage(redo_lists_size);
		List<Redo> redo_list = (List<Redo>) allDAO.getRedoDAO().findhql(hql);
		json = "{\"total\":" + page.getCountNums() + "," +
				"\"rows\":[";
		for(Redo redo : redo_list){
			json = json.concat("{\"companyNum\":\"" + redo.getCompanyNum() + "\"," +
					"\"department\":\"" + redo.getDepartment() + "\"," +
					"\"redoNum\":\"" + redo.getRedoNum() + "\"" +
					"},");
		}
		if(redo_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]}");
		else
			json = json.concat("]}");
		return json;
	}
	
	
	/**
	 * 详细重做信息
	 */
	@SuppressWarnings("unchecked")
	public String get_redo_see(String companyNum, String department){
		String json = "";
		List<Trace> trace_list = (List<Trace>) allDAO.getTraceDAO().findhql("from Trace t where t.companyNum = '"+companyNum+"' and t.department = '"+department+"' order by t.regtime ASC");
		json = "{\"total\":0 ," +
				"\"rows\":[";
		String com = "";
		int i = 0;
		for(Trace t : trace_list){
			i++;
			if(t.getOuttime()!=null) com = t.getOuttime();
			json = json.concat("{\"dep\":\"第" + i + "次重做\"," +
					"\"regtime\":\"" + t.getRegtime() + "\"," +
					"\"outtime\":\"" + com + "\"" +
					"},");
		}
		if(trace_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]}");
		else
			json = json.concat("]}");
		return json;
	}
	
	
	/**
	 * 全部部门 重做统计
	 */
	@SuppressWarnings("unchecked")
	public String get_redo_stat(String begin_time, String end_time){
		if(begin_time.length()==0&&end_time.length()!=0){
			begin_time = "2012-10-1";
		}
		if(begin_time.length()!=0&&end_time.length()==0){
			Date date = new Date();
			String now = df.format(date);
			end_time = now;
		}
		if(begin_time.length()==0&&end_time.length()==0){
			Date date = new Date();
			String now = df.format(date);
			begin_time = "2012-10-1";
			end_time = now;
		}
		
		String json = "";
		List<Dep> dep_list = (List<Dep>) allDAO.getDepDAO().findhql("from Dep d order by d.num ASC");
		int p = dep_list.size()/6;
		if(p==0) p=1;
		json = "{\"total\":0 ," +
				 "\"title\":\""+begin_time+"----"+end_time+"\","+
				 "\"x\":\"部门\","+
				 "\"n\":\""+p+"\","+
				"\"rows\":\"[";
		for(Dep d : dep_list){
			String string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+begin_time+"' and '"+end_time+"'))";
			String hql = "from Redo r" +" "+ "where " + "r.department =" + " " + "'" + d.getDepartment()+ "'"+" "+ string_order;
			List<Redo> redo_list = (List<Redo>) allDAO.getRedoDAO().findhql(hql);
			int redos = 0;
			for(Redo r : redo_list){
				redos = redos + Integer.valueOf(r.getRedoNum());
			}
			json = json.concat("['"+d.getDepartment()+"'," + redos + "],");
		}
		
		if(dep_list.size() != 0)
			json = json.substring(0, json.length()-1).concat("]\"}");
		else
			json = json.concat("\"}");
		return json;
	}
	
	
	/**
	 * 单一部门 重做统计
	 */
	@SuppressWarnings("unchecked")
	public String get_redo_stat(String department, String begin_time, String end_time){
		if(begin_time.length()==0&&end_time.length()!=0){
			begin_time = "2012-10-1";
		}
		if(begin_time.length()!=0&&end_time.length()==0){
			Date date = new Date();
			String now = df.format(date);
			end_time = now;
		}
		if(begin_time.length()==0&&end_time.length()==0){
			Date date = new Date();
			String now = df.format(date);
			begin_time = "2012-10-1";
			end_time = now;
		}
		
		String json = "";
		String[] sb = begin_time.split("-");
		String[] se = end_time.split("-");
		int n = Integer.valueOf(se[0]) - Integer.valueOf(sb[0]);
		int y = Integer.valueOf(se[1]) - Integer.valueOf(sb[1]);
		int a = n*12+y+1;
		ArrayList<String> com=new ArrayList<String>(); 
		if(n==0&&a>=0){
			for(int i=0;i<a;i++){
				com.add(sb[0] +"-"+ String.valueOf(Integer.valueOf(sb[1])+i));
			}
		}
		else if(n>0&&a>=0){
			for(int i=0;i<12-Integer.valueOf(sb[1])+1;i++){
				com.add(sb[0] +"-"+ String.valueOf(Integer.valueOf(sb[1])+i));
			}
			for(int i=0 ; i<a-(12-Integer.valueOf(sb[1])+1) ;i++){
				com.add(String.valueOf(Integer.valueOf(sb[0])+1) +"-"+ String.valueOf(1+i));
			}
		}
		int p = a/6;
		if(p==0) p=1;
		json = "{\"total\":0 ," +
				"\"title\":\""+department+"\","+
				 "\"x\":\"月份\","+
				 "\"n\":\""+p+"\","+
				"\"rows\":\"[['0',0],";
		String sbegin_time = "";
		String send_time = "";
		String string_order = "";
		String hql = "";
		
		Iterator<String> com_iterator=com.iterator();
		if(com_iterator.hasNext()){
			String st = com_iterator.next();
			begin_time = st+"-"+sb[2];
			end_time = st+"-31";
			string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+sbegin_time+"' and '"+send_time+"'))";
			hql = "from Redo r" +" "+ "where " + "r.department =" + " " + "'" + department+ "'"+" "+ string_order;
			List<Redo> redo_list = (List<Redo>) allDAO.getRedoDAO().findhql(hql);
			int redos = 0;
			for(Redo r : redo_list){
				redos = redos + Integer.valueOf(r.getRedoNum());
			}
			json = json.concat("['"+begin_time+"——31'," + redos + "],");
		}
		
		while(com_iterator.hasNext()){
			String st = com_iterator.next();
			sbegin_time = st+"-1";
			if(com_iterator.hasNext()){
				send_time = st+"-31";
			}else{
				send_time = st+"-"+se[2];
			}
			string_order = "and r.companyNum in(select o.companyNum from OrderFrom o where  " + "(o.makeTime between '"+sbegin_time+"' and '"+send_time+"'))";
			hql = "from Redo r" +" "+ "where " + "r.department =" + " " + "'" + department+ "'"+" "+ string_order;
			List<Redo> redo_list = (List<Redo>) allDAO.getRedoDAO().findhql(hql);
			int redos = 0;
			for(Redo r : redo_list){
				redos = redos + Integer.valueOf(r.getRedoNum());
			}
			if(com_iterator.hasNext()){
				json = json.concat("['"+st+"'," + redos + "],");
			}else{
				json = json.concat("['"+st+"-1——"+se[2]+"'," + redos + "],");
			}
			
		}
		
		if(com.size() != 0)
			json = json.substring(0, json.length()-1).concat("]\"}");
		else
			json = json.concat("\"}");
		return json;
	}
	
	
	
	public AllDAO getAllDAO() {
		return allDAO;
	}

	@Resource(name = "allDAO")
	public void setAllDAO(AllDAO allDAO) {
		this.allDAO = allDAO;
	}
}
